provider "alicloud" {
  profile = "default"
  version = "1.151.0"
}

#
# 0. 定义全局变量、标签、名称
#
variable "k8s_name_prefix" {
  description = "The name prefix used to create managed kubernetes cluster."
  default     = "tf-ack"
}

# 1. vpc
module "vpc" {
  source  = "./vpc"

  vpc_name = local.tags.app
  vpc_cidr          = "10.10.0.0/16"
  vpc_description = "测试vpc"
  availability_zones = ["cn-hangzhou-e", "cn-hangzhou-f", "cn-hangzhou-g"]
  vpc_tags = local.tags
  vswitch_cidrs      = ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"]
  vswitch_name      = local.tags.app
  vswitch_tags = local.tags
}

# 2. nat & snat
resource "alicloud_nat_gateway" "main" {
  vpc_id               = module.vpc.vpc_id
  name                 = local.tags.app
  vswitch_id           = module.vpc.vswitch_ids[2]
  nat_type             = "Enhanced"
  tags                 = local.tags
}

module eip {
  source           = "./eip"
  name     = local.tags.app
  tags = local.tags
}

resource "alicloud_eip_association" "default" {
  allocation_id = module.eip.id[0]
  instance_id   = alicloud_nat_gateway.main.id
  depends_on = [module.eip, alicloud_nat_gateway.main]
}

resource "alicloud_snat_entry" "default" {
  count = length(module.vpc.vswitch_ids)
  snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
  source_vswitch_id = module.vpc.vswitch_ids[count.index]
  snat_ip           = module.eip.ip_address[0]
  depends_on        = [alicloud_eip_association.default]
}

# k8s
module "sls" {
  source  = "./sls"
  log_project_name = local.log_project_name
  tags             = local.tags
}

# Kubernetes托管版。
data "alicloud_instance_types" "default" {
  cpu_core_count       = 4
  memory_size          = 8
  kubernetes_node_role = "Worker"
}

variable "cluster_addons" {
  description = "Addon components in kubernetes cluster"
  type = list(object({
    name   = string
    config = string
  }))
  default = [
    {
      name   = "logtail-ds",
      config = "{\"IngressDashboardEnabled\":\"true\",\"sls_project_name\":alicloud_log_project.log.name}}",
    },
    {
      name   = "nginx-ingress-controller",
      config = "{\"IngressSlbNetworkType\":\"internet\"}",
    }
  ]
}

//resource "alicloud_cs_managed_kubernetes" "default" {
//  name                      = local.k8s_name
//  worker_vswitch_ids        = module.vpc.vswitch_ids
//  new_nat_gateway           = false
//  worker_instance_types     = [data.alicloud_instance_types.default.instance_types[0].id]
//  worker_number             = 2
//  password                  = "Yourpassword1234"
//  pod_cidr                  = "172.20.0.0/16"
//  service_cidr              = "172.21.0.0/20"
//  install_cloud_monitor     = true
//  # 是否为API Server创建Internet负载均衡。默认为false。
//  slb_internet_enabled      = true
//  # 节点的系统磁盘类别。其有效值为cloud_ssd和cloud_efficiency。默认为cloud_efficiency。
//  worker_disk_category      = "cloud_efficiency"
//  # 节点的数据磁盘类别。其有效值为cloud_ssd和cloud_efficiency，如果未设置，将不会创建数据磁盘。
//  worker_data_disk_category = "cloud_ssd"
//  worker_data_disk_size     = 200
//
//  dynamic "addons" {
//    for_each = var.cluster_addons
//    content {
//      name   = lookup(addons.value, "name", var.cluster_addons)
//      config = lookup(addons.value, "config", var.cluster_addons)
//    }
//  }
//}

# 3. eip & slb
module "rds" {
  source     = "./rds"
  vswitch_id = module.vpc.vswitch_ids[2]
  zone_id    = module.vpc.availability_zones[2]
}

# 4. rds
//module "rds" {
//  source  = "./rds"
//  vswitch_id = module.vpc.vswitch_ids
//}

# 
# 6. redis
#
//module "ack" {
//  source  = "./ack"
//  vswitch_id = module.vpc.vswitch_ids
//}

# 
# 7. oss
#
//module "ack" {
//  source  = "./ack"
//  vswitch_id = module.vpc.vswitch_ids
//}

#
# 8. dns
#
//module "dns" {
//  source  = "./ack"
//  vswitch_id = module.vpc.vswitch_ids
//}

#
# 9. kafka
#
//module "dns" {
//  source  = "./ack"
//  vswitch_id = module.vpc.vswitch_ids
//}

#
# 10. mse
#
//module "dns" {
//  source  = "./ack"
//  vswitch_id = module.vpc.vswitch_ids
//}

#
# ack
#
//module "ack" {
//  source  = "./ack"
//  vswitch_id = module.vpc.vswitch_ids
//}